import { _decorator, assetManager } from 'cc';
import { levelConfig } from '../Config/Config';
const { ccclass, property } = _decorator;

@ccclass('SS_AssetsManager')
export default class SS_AssetsManager {
        static Levels: any[] = [];

        public static Load_Level() {
                return new Promise<void>(async resolve => {
                        assetManager.loadBundle('Level', (err, bundle) => {
                                bundle.loadDir("", function (err, assets) {
                                        for (const iterator of assets) {
                                                // SS_AssetsManager.Levels.push(iterator.json);
                                                // console.log("iterator.name", iterator.name);

                                                let item = levelConfig.find(item => item.name === iterator.name);
                                                // console.log("item", item);
                                                // console.log("item", item.id);
                                                let index = levelConfig.indexOf(item);
                                                // console.log("index", index);

                                                SS_AssetsManager.Levels[index] = iterator.json
                                        }

                                        // console.log("SS_AssetsManager.Levels", SS_AssetsManager.Levels);
                                        resolve();
                                });
                        });
                });
        }

        public static Load_Sprite() {
                return new Promise<void>(async resolve => {
                        assetManager.loadBundle('Sprite', (err, bundle) => {
                                resolve();

                                bundle.loadDir("main/item/icon", function (err, assets) {
                                });
                        });
                });
        }

        public static Load_Prefab() {
                return new Promise<void>(async resolve => {
                        assetManager.loadBundle('Prefab', (err, bundle) => {
                                resolve();
                        });
                });
        }

        public static async Load_All() {
                let promise_Sprite = SS_AssetsManager.Load_Sprite();
                let promise_Prefab = SS_AssetsManager.Load_Prefab();
                let promise_Level = SS_AssetsManager.Load_Level();

                //        // Promise.all([promise_Sprite, promise_Prefab]).then((values) => {
                //        //     console.log(values);
                //        // });

                await Promise.all([promise_Sprite, promise_Prefab, promise_Level]);
        }
}

/** 调试 */
window["SS_AssetsManager"] = SS_AssetsManager;